package com.wbdgj.ui.news;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.services.geocoder.GeocodeAddress;
import com.amap.api.services.geocoder.GeocodeQuery;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.base.BaseFragment;
import com.wbdgj.base.BaseWhiteFragment;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnNoResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.home.ExampleActivity;
import com.wbdgj.ui.home.ExampleListActivity;
import com.wbdgj.ui.home.LoactionActivity;
import com.wbdgj.ui.home.NewsActivity;
import com.wbdgj.ui.home.NewsAllActivity;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.zaaach.citypicker.CityPickerActivity;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * Created by Administrator on 2018/5/2.
 */

public class NewsFragment extends BaseWhiteFragment {

    private Context context;
    @BindView(R.id.destination_tab_layout)
    TabLayout mTabLayout;
    @BindView(R.id.destination_viewpager)
    ViewPager mViewPager;

    private MyFragmentPagerAdapter adapter;
    private List<Fragment> fragments;
    private News01Fragment News01Fragment;
    private News02Fragment News02Fragment;
    private News03Fragment News03Fragment;

    @BindView(R.id.tv_leftTitle)
    TextView tv_leftTitle;
    @BindView(R.id.pointXx)
    ImageView pointXx;

    @Override
    protected int getLayout() {
        return R.layout.ui_news;
    }

    @Override
    protected void onInit() {
        context = getActivity();

        fragments = new ArrayList<>();
        fragments.add(News01Fragment = (News01Fragment) Fragment.instantiate(context,News01Fragment.class.getName()));
        fragments.add(News02Fragment = (News02Fragment)Fragment.instantiate(context,News02Fragment.class.getName()));
        fragments.add(News03Fragment = (News03Fragment)Fragment.instantiate(context,News03Fragment.class.getName()));
        adapter = new MyFragmentPagerAdapter(getChildFragmentManager());
        mViewPager.setAdapter(adapter);

        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.getTabAt(0).setText("推荐");
        mTabLayout.getTabAt(1).setText("公告");
        mTabLayout.getTabAt(2).setText("资讯");

        findViewById(R.id.tv_rightImg).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                launch(NewsAllActivity.class);
            }
        });
    }

    @Override
    public void fetchData() {

    }

    private class MyFragmentPagerAdapter extends FragmentPagerAdapter {
        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);

        }
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        appmbmsgcnt();
    }

    private void appmbmsgcnt() {
        Call<ResultObjectModel> call01 = HttpAdapter.getSerives().appmbmsgcnt(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call01.enqueue(new OnNoResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if (decimalFormat.format(linkedTreeMap.get("CNT")).equals("0")){
                        pointXx.setVisibility(View.GONE);
                    }else{
                        pointXx.setVisibility(View.VISIBLE);
                    }
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
    }
}
